package com.spark.colliery.widget

import android.content.Context
import android.database.DataSetObserver
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.animation.AnimationUtils
import android.widget.ListAdapter
import android.widget.ViewFlipper

import com.blankj.utilcode.util.LogUtils
import com.spark.colliery.R

/**
 * 纵向自动滚动
 */

class VerticalScrollLayout(context: Context, attrs: AttributeSet? = null) : ViewFlipper(context, attrs) {

    private var mAdapter: ListAdapter? = null
    private var isSetAnimDuration = false
    private var interval = 3000
    /**
     * 动画时间
     */
    private var animDuration = 800

    private val mDataObserver = object : DataSetObserver() {

        override fun onChanged() {
            setupChildren()
        }

        override fun onInvalidated() {
            setupChildren()
        }

    }

    init {
        val array = context.obtainStyledAttributes(attrs, R.styleable.VerticalScrollLayout)
        animDuration = array.getInt(R.styleable.VerticalScrollLayout_vsl_animDuration, animDuration)
        isSetAnimDuration = array.getBoolean(R.styleable.VerticalScrollLayout_vsl_isCusDuration, false)
        interval = array.getInt(R.styleable.VerticalScrollLayout_vsl_sleepTime, interval)
        array.recycle()
        setFlipInterval(interval)
        val animIn = AnimationUtils.loadAnimation(context, R.anim.anim_scroll_in)
        val animOut = AnimationUtils.loadAnimation(context, R.anim.anim_scroll_out)
        if (isSetAnimDuration) {
            animIn.duration = animDuration.toLong()
            animOut.duration = animDuration.toLong()
        }
        inAnimation = animIn
        outAnimation = animOut


        for (i in 0 until childCount) {
            val childAt = getChildAt(i)
            childAt?.setOnTouchListener { v, event ->
                when (event.action) {
                    MotionEvent.ACTION_DOWN, MotionEvent.ACTION_MOVE -> {
                        LogUtils.e("isFlipping = $isFlipping")
                        if (isFlipping) {
                            stopFlipping()
                        }
                    }
                    MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
                        LogUtils.e("isFlipping = $isFlipping")
                        if (!isFlipping) {
                            startFlipping()
                        }
                    }
                }
                true
            }
        }
    }


    fun setAdapter(adapter: ListAdapter) {
        if (mAdapter != null) {
            mAdapter!!.unregisterDataSetObserver(mDataObserver)
        }

        mAdapter = adapter

        if (mAdapter != null) {
            mAdapter!!.registerDataSetObserver(mDataObserver)
        }
        setupChildren()
    }

    private fun setupChildren() {
        if (mAdapter == null || mAdapter!!.count == 0) return
        removeAllViews()
        for (i in 0 until mAdapter!!.count) {
            val child = mAdapter!!.getView(i, null, this)
            if (child == null) {
                throw NullPointerException("View can't be null")
            } else {
                addView(child)
            }
        }
        startFlipping()
    }

    override fun onInterceptTouchEvent(ev: MotionEvent): Boolean {
        when (ev.action) {
            MotionEvent.ACTION_DOWN -> if (isFlipping) {
                stopFlipping()
            }
            MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> if (!isFlipping) {
                startFlipping()
            }
        }
        return super.onInterceptTouchEvent(ev)
    }

    override fun onTouchEvent(event: MotionEvent): Boolean {
        when (event.action) {
            MotionEvent.ACTION_DOWN -> if (isFlipping) {
                stopFlipping()
            }
            MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> if (!isFlipping) {
                startFlipping()
            }
        }

        return super.onTouchEvent(event)
    }
}
